package com.qfhd.soft.cjzcore.UI.MulitRecycle.viewholder;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Model.Task_image_click_type;
import com.qfhd.soft.cjzcore.R;
import com.qfhd.soft.cjzcore.UI.MulitRecycle.bean.Task_image_submit_bean;

public class Task_image_submit_viewHolder extends BaseViewHolder {

    //    @BindView(R.id.task_image_submit_textType)
    private RelativeLayout textType_linearL;
    //    @BindView(R.id.task_image_submit_upImageType)
    private RelativeLayout upImage_RelativeL;
    //    @BindView(R.id.task_image_submit_textType_tip)
    private TextView tx_textType_tip;
    //    @BindView(R.id.task_image_submit_textType_editText)
    private EditText edit_text;
    //    @BindView(R.id.task_image_submit_upImageType_tip)
    private TextView tx_upImage_tip;
    //    @BindView(R.id.task_image_submit_upImageType_image)
    private ImageView img_upimage;
    //    @BindView(R.id.task_image_submit_upImage_shili)
    private ImageView img_shili;
    //    @BindView(R.id.task_image_submit_upImageType_close)
    private RelativeLayout close_relativieL;
    //    @BindView(R.id.task_image_submit_upImageType_btn)
    private Button btn_upimage_add;

    public Task_image_submit_viewHolder(View itemView) {
        super(itemView);
//        ButterKnife.bind(this,itemView);
        textType_linearL = (RelativeLayout) itemView.findViewById(R.id.task_image_submit_textType);
        upImage_RelativeL = (RelativeLayout) itemView.findViewById(R.id.task_image_submit_upImageType);
        tx_textType_tip = (TextView) itemView.findViewById(R.id.task_image_submit_textType_tip);
        edit_text = (EditText) itemView.findViewById(R.id.task_image_submit_textType_editText);
        tx_upImage_tip = (TextView) itemView.findViewById(R.id.task_image_submit_upImageType_tip);
        img_upimage = (ImageView) itemView.findViewById(R.id.task_image_submit_upImageType_image);
        img_shili = (ImageView) itemView.findViewById(R.id.task_image_submit_upImage_shili);
        close_relativieL = (RelativeLayout) itemView.findViewById(R.id.task_image_submit_upImageType_close);
        btn_upimage_add = (Button) itemView.findViewById(R.id.task_image_submit_upImageType_btn);

    }

    @Override
    public void bindViewData(Object data) {
        Task_image_submit_bean bean = (Task_image_submit_bean) data;

        if (bean.getModel().type == 1) {
            //字符串
            textType_linearL.setVisibility(View.VISIBLE);
            upImage_RelativeL.setVisibility(View.GONE);
            textTypeInit(bean);
        } else if (bean.getModel().type == 2) {
            //图片
            textType_linearL.setVisibility(View.GONE);
            upImage_RelativeL.setVisibility(View.VISIBLE);
            upImageTypeInit(bean);
        }
    }

    private void textTypeInit(Task_image_submit_bean bean) {
        tx_textType_tip.setText(bean.getModel().title + ":");
        edit_text.setHint(bean.getModel().tip);
        edit_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bean.getModel().textInput = s.toString();
                CJLogger.v("afterTextChanged:" + s.toString());
            }
        });
    }

    private void upImageTypeInit(Task_image_submit_bean bean) {
        tx_upImage_tip.setText(bean.getModel().title);
        Glide.with(bean.getmContext()).load(bean.getModel().exampleImage)
                .placeholder(bean.getmContext().getResources().getDrawable(R.drawable.placeholder))
                .into(img_shili);

        if (TextUtils.isEmpty(bean.getModel().upImageUrl)) {
            btn_upimage_add.setText("添加");
            btn_upimage_add.setBackground(bean.getmContext().getResources().getDrawable(R.drawable.radius_20_theme));
            btn_upimage_add.setEnabled(true);
            img_upimage.setImageDrawable(bean.getmContext().getResources().getDrawable(R.drawable.empty_placeholder));
            close_relativieL.setVisibility(View.GONE);
        } else {
            Glide.with(bean.getmContext()).load(bean.getModel().upImageUrl)
                    .placeholder(bean.getmContext().getResources().getDrawable(R.drawable.empty_placeholder))
                    .into(img_upimage);
            btn_upimage_add.setText("已添加");
            btn_upimage_add.setBackground(bean.getmContext().getResources().getDrawable(R.drawable.radius_20_gray));
            btn_upimage_add.setEnabled(false);
            close_relativieL.setVisibility(View.VISIBLE);
        }

        btn_upimage_add.setOnClickListener(v -> {
            if (bean.getListener() != null) {
                bean.getListener().onClick(Task_image_click_type.AddImage);
            }
        });

        close_relativieL.setOnClickListener(v -> {
            if (bean.getListener() != null) {
                bean.getListener().onClick(Task_image_click_type.ClearImage);
            }
        });

        img_upimage.setOnClickListener(v -> {
            if (bean.getListener() != null) {
                bean.getListener().onClick(Task_image_click_type.ShowImage);
            }
        });

        img_shili.setOnClickListener(v -> {
            if (bean.getListener() != null) {
                bean.getListener().onClick(Task_image_click_type.ShowShiLiImage);
            }
        });
    }
}
